package com.weng.cloud.service8881.mvc;

import com.cloud.api.mock.MockArgDto;
import com.cloud.api.mock.MockResDto;
import com.weng.cloud.commons.base.JsonUtil;
import com.weng.cloud.commons.base.TimeUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: weng
 * @Date: 2022/6/19
 * @Description: com.weng.cloud.service8881.mvc
 */
@RestController
@RequestMapping("/mvc")
public class DateTimeFormatController {


    /*
        坑1：
            jackson.serialization
                wrap_root_value --> {"content": "weng测试内容"} --> {"argDto":{"content": "weng测试内容"}}
                fail_on_empty_beans --> {"content": "weng测试内容"} --> {"content": "weng测试内容",date:"2022-06-20"}
     */

    /*
        坑2：
            Date类型默认转换 yyyy-MM-dd'T'HH:mm:ss.SSSX(如果输入 yyyy-MM-dd --> 时分秒默认 8:00:00 AM)
            走json转换 -> @JsonFormat
            不走 -> @DateTimeFormat (比如说 get请求(Date date)) org.springframework.core.convert.ConversionFailedException
     */

//    @RequestMapping(value = "/dateTimeFormat",method = RequestMethod.POST)
    @RequestMapping(value = "/dateTimeFormat",method = RequestMethod.GET)
    public MockArgDto dateTimeFormat(MockArgDto argDto){
        System.err.println(JsonUtil.toText(argDto));
//        return doDateTimeFormat(argDto);
        return argDto;
    }

    private MockResDto doDateTimeFormat(MockArgDto argDto) {
        return MockResDto.builder()
                .content(argDto.getContent())
                .year(TimeUtil.year(argDto.getDate()))
                .month(TimeUtil.month(argDto.getDate()))
                .build();
    }
}
